package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsAd;
import com.uyayo.cms.entity.CmsAdCategory;
import com.uyayo.cms.mapper.CmsAdCategoryMapper;
import com.uyayo.cms.model.CmsAdCategoryCondition;
import com.uyayo.cms.model.CmsAdCondition;
import com.uyayo.cms.service.CmsAdCategoryService;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("cmsAdCategoryService")
public class CmsAdCategoryServiceImpl extends BaseServiceImpl<CmsAdCategory> implements CmsAdCategoryService {

	private final CmsAdCategoryMapper cmsAdCategoryMapper;

	public CmsAdCategoryServiceImpl(CmsAdCategoryMapper cmsAdCategoryMapper) {
		this.cmsAdCategoryMapper = cmsAdCategoryMapper;
	}

	public List<CmsAdCategory> selectCmsAdCategory(CmsAdCategoryCondition cmsAdCategoryCondition) {
		Example example = new Example(CmsAdCategory.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(cmsAdCategoryCondition);
		example.createCriteria();
		return cmsAdCategoryMapper.selectByExample(example);
	}

	@Override
	public PageBean<CmsAdCategory> selectAdCategoryByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsAdCategoryCondition cmsAdCategoryCondition) {

		List<CmsAdCategory> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);

		Example example = new Example(CmsAdCategory.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Example.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(cmsAdCategoryCondition.getName())){
			criteria.andLike("name","%"+ cmsAdCategoryCondition.getName()+"%");
		}
		if(!StringUtils.isEmpty(cmsAdCategoryCondition.getCode())){
			criteria.andEqualTo("code", cmsAdCategoryCondition.getCode());
		}
		if(cmsAdCategoryCondition.getBeginCreateTime()!=null || cmsAdCategoryCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", cmsAdCategoryCondition.getBeginCreateTime(), cmsAdCategoryCondition.getEndCreateTime());
		}
		if(Objects.nonNull(cmsAdCategoryCondition.getStatus())){
			criteria.andEqualTo("status",cmsAdCategoryCondition.getStatus());
		}
		userList = cmsAdCategoryMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsAdCategory> pageInfo = new PageInfo<>(userList);

		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}
}
